import os
import random
import time

import pygame
import sys

from game.snake.entity.block import Block
from game.snake.utils.block_action import BlockAction
from game.snake.utils.utils import Utils
from game.zhuizong.entity.Circle import Circle

pygame.init()
#设置表体
pygame.display.set_caption('追综')
screen = pygame.display.set_mode((800, 600))
# 移动步长全局变量
step = 50
speed = 1
# 设置背景颜色为淡蓝色
bg_color = (255, 255, 255)
# 屏幕大小
width_screen = 20
height_screen = 15
screen = pygame.display.set_mode((step*width_screen, step*height_screen))


def newCircle(circle_x,circle_y,circle_radius,circle_color,screen,textt):
    circle = Circle(circle_x, circle_y, circle_radius, circle_color, screen)
    text = random.randint(0,9)
    circle.text = str(textt)
    return circle

# 扣减绿球分数
def delscore_green(result,score_green,circle_red_1):
    if result:
        score_green = score_green - 1
        text = circle_red_1.text
        # 将text转换为数字，并且增加1
        score = 1 + int(text)
        circle_red_1.text = str(score)
    return score_green

def game_loop():
    screen.fill(bg_color)

    # 设置圆形参数
    circle_color = (255, 0, 0)  # 红色
    circle_color_green = (0, 255, 0)  # 绿色
    circle_color_blue = (0, 0, 255)  # 蓝色
    circle_radius = 15  # 半径
    # 开始时间
    time_start = time.time()
    print(time_start)
    # 追踪小球集合
    circle_red_group = []
    circle_red_group.append(newCircle(100, 200, circle_radius, circle_color, screen,1))
    circle_red_group.append(newCircle(110, 235, circle_radius, circle_color_green, screen,2))
    circle_red_group.append(newCircle(105, 200, circle_radius, circle_color_blue, screen,3))
    # 目标小球
    circle_green = newCircle(random.randint(0,screen.get_width()), random.randint(0,screen.get_height()), 20, (200,200,200), screen,100)
    circle_green.text = "100"
    score_green = 100;
    speed_add_green = False



    while True:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()


        # 设置背景颜色
        screen.fill(bg_color)
        # 绘制圆形
        # pygame.draw.circle(screen, circle_color, circle_center, circle_radius)
        for circleitem in circle_red_group:
            circleitem.draw(pygame)

        circle_green.draw(pygame)
        # 更新屏幕显示
        pygame.display.update()
        # 设置刷新频率,数字越大，刷新越频繁
        pygame.time.Clock().tick(60)
        # 绿球被吃掉后，加速逃跑
        if int(circle_green.text) < 0 or speed_add_green:
            circle_green.move(15,15)
            score_green = score_green+10
            if score_green > 100:
                speed_add_green = False
            else:
                speed_add_green = True
        else:
            circle_green.move(2, 2)


        # 追踪小球
        for circleitem in circle_red_group:
            circleitem.track(circle_green,False)
        # 让circle_red_1与circle_red_2保持距离，避免重叠
        # 遍历circle_red_group，计算两两之间互不碰撞
        print("=====")
        isc = True
        while isc:
            isctemp = False
            for i in range(len(circle_red_group)):
                for j in range(i+1,len(circle_red_group)):
                    print("i:",i,"j:",j)
                    circleitem1 = circle_red_group[i]
                    circleitem2 = circle_red_group[j]
                    resultCollision = circleitem1.isCollision4r(circleitem2)
                    if resultCollision:
                        circle_red_group[i].move(-1,-1)
                        isctemp = True
                if i>1:
                    for m in range(0,i-1):
                        circleitem1 = circle_red_group[m]
                        circleitem2 = circle_red_group[i]
                        resultCollision = circleitem1.isCollision4r(circleitem2)
                        if resultCollision:
                            circle_red_group[m].move(-1,-1)
                            isctemp = True
            isc = isctemp

        # 追到绿球后得分
        for circleitem in circle_red_group:
            resultCollision = circleitem.isCollision(circle_green)
            score_green = delscore_green(resultCollision,score_green,circleitem)


        circle_green.text = str(score_green)
# 文件路径
if __name__ == '__main__':
    game_loop()
